// Copyright (C) 2022 Huawei Device Co., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package services

import (
	"dist/dao"
	"dist/model"
	"dist/utils"
	"errors"
	"time"
)

func HandleRecords(record model.RecordReq) error {
	timeUnix := time.UnixMilli(record.Ts)
	result := dao.InsertRecord(model.Record{Ts: timeUnix, Action: record.Action, Event: record.Event, ExpandId: -1})
	if result {
		return nil
	} else {
		return errors.New(" insert record failed")
	}
}

func PageDistributionService(startTime time.Time, endTime time.Time) []model.PageDistributionStruct {
	return dao.GetPageDistributionCount(startTime, endTime)
}

func ImportTraceService(startTime time.Time, endTime time.Time, interCode int) []model.ChartData {
	interval := getIntervalStr(interCode)
	return dao.GetImportTraceCount(startTime, endTime, interval)
}

func PageUse(startTime time.Time, endTime time.Time, interCode int, action string) []model.ChartData {
	interval := getIntervalStr(interCode)
	return dao.GetPageUseCount(startTime, endTime, interval, action)
}

func PluginsUse(startTime time.Time, endTime time.Time) []model.PluginsData {
	return dao.GetPluginsUse(startTime, endTime)
}

func CmdUse(startTime time.Time, endTime time.Time, interCode int) model.CmdUseData {
	interval := getIntervalStr(interCode)
	onlineUse := getUseByEvent(startTime, endTime, interval, "config_page", "online_record")
	offlineUse := getUseByEvent(startTime, endTime, interval, "config_page", "offline_record")
	return model.CmdUseData{Offline: offlineUse, Online: onlineUse}
}

func DownloadUse(startTime time.Time, endTime time.Time, interCode int) model.DownloadData {
	interval := getIntervalStr(interCode)
	downloadFile := getUseByEvent(startTime, endTime, interval, "download", "download")
	downloadDB := getUseByEvent(startTime, endTime, interval, "download", "download_db")
	return model.DownloadData{DownLoadDB: downloadDB, DownLoadFile: downloadFile}
}

func getUseByEvent(startTime time.Time, endTime time.Time, interval string, action string, event string) []model.ChartData {
	return dao.GetUse(startTime, endTime, interval, action, event)
}

func GetRowAndTabCount(startTime time.Time, endTime time.Time, interCode int) []model.RowData {
	interval := getIntervalStr(interCode)
	return dao.GetRowAndTabCount(startTime, endTime, interval)
}

func GetRowAndTabSubCount(startTime time.Time, endTime time.Time, interCode int) []model.RowData {
	interval := getIntervalStr(interCode)
	return dao.GetRowAndTabSubCount(startTime, endTime, interval)
}

func HandleGeneralRecord(record model.GeneralRecordReq) error {
	timeUnix := time.UnixMilli(record.Ts)
	if len(record.Catgory) != 0 && len(record.SecondCat) != 0 && len(record.ThirdCat) != 0 {
		for _, thirdCat := range record.ThirdCat {
			result := dao.InsertGeneralRecord(model.GeneralRecord{Ts: timeUnix, Category: record.Catgory, SecondCat: record.SecondCat, ThirdCat: thirdCat})
			if !result {
				return errors.New(" insert record failed")
			}
		}
	} else if len(record.Catgory) != 0 && len(record.SecondCat) != 0 {
		result := dao.InsertGeneralRecord(model.GeneralRecord{Ts: timeUnix, Category: record.Catgory, SecondCat: record.SecondCat, ThirdCat: "-"})
		if !result {
			return errors.New(" insert record failed")
		}
	} else if len(record.Catgory) != 0 {
		result := dao.InsertGeneralRecord(model.GeneralRecord{Ts: timeUnix, Category: record.Catgory, SecondCat: "-", ThirdCat: "-"})
		if !result {
			return errors.New(" insert record failed")
		}
	} else {
		return errors.New(" no data need insert Or format error")
	}
	return nil
}

func HandleGeneralQuery(req model.GeneralQueryReq) []model.GeneralRecordData {
	interval := getIntervalStr(req.Interval)
	startTime := utils.TsToTime(req.StartTime)
	endTime := utils.TsToTime(req.EndTime)
	if len(req.Catgory) != 0 && len(req.SecondCat) != 0 && len(req.ThirdCat) != 0 {
		return dao.GetGeneralRecordUse3(req, startTime, endTime, interval, req.Catgory, req.SecondCat, req.ThirdCat)
	} else if len(req.Catgory) != 0 && len(req.SecondCat) != 0 {
		return dao.GetGeneralRecordUse2(req, startTime, endTime, interval, req.Catgory, req.SecondCat)
	} else if len(req.Catgory) != 0 {
		return dao.GetGeneralRecordUse(req, startTime, endTime, interval, req.Catgory)
	}
	return nil
}
